#include <cmath>
#include <iostream>

#include "Point.h"

using std::cout;
using std::endl;

Point::Point()
    :_ix(0)
     ,_iy(0){}

Point::Point(int x, int y)
    :_ix(x)
     ,_iy(y){}

     double Point::pointLength() const{
         return hypot(this->_ix, this->_iy);
     }

int Point::getX()const{
    return this->_ix;
}

int Point::getY()const{
    return this->_iy;
}

bool operator<(const Point & lhs, const Point & rhs){
    if(lhs.pointLength() < rhs.pointLength()){
        return true;
    }

    if(lhs.pointLength() == rhs.pointLength()){
        if(lhs._ix < rhs._ix)
        {
            return true;
        }
        else if(lhs._ix == rhs._ix)
        {
            if(lhs._iy < rhs._iy)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}

std::ostream &operator<<(std::ostream & os, const Point & p){
    os << "Point(" << p._ix << "," << p._iy << ")";

    return os;
}

bool ComparePoint::operator()(const Point &lhs, const Point &rhs) const
{
    cout << "struct ComparePoint" << endl;
    if(lhs.pointLength() < rhs.pointLength())
    {
        return true;
    }
    else if(lhs.pointLength() == rhs.pointLength())
    {
        if(lhs._ix < rhs._ix)
        {
            return true;
        }
        else if(lhs._ix == rhs._ix)
        {
            if(lhs._iy < rhs._iy)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return false;
    }
}
//命名空间的扩展
namespace std
{
    bool less<Point>::operator()( const Point& lhs, const Point& rhs ) const
    {
        cout << "template <> struct less<Point>" << endl;
        if(lhs.pointLength() < rhs.pointLength())
        {
            return true;
        }
        else if(lhs.pointLength() == rhs.pointLength())
        {
            if(lhs.getX() < rhs.getX())
            {
                return true;
            }
            else if(lhs.getX() == rhs.getX())
            {
                if(lhs.getY() < rhs.getY())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
}//end of namespace std
